package com.ruoyi.product.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.sql.QueryHelp;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.product.domain.FaultType;
import com.ruoyi.product.domain.WoOpLog;
import com.ruoyi.product.domain.WoOpLog;
import com.ruoyi.product.domain.dto.WoOpLogDto;
import com.ruoyi.product.domain.vo.WoOpLogVo;
import com.ruoyi.product.domain.vo.WoOpLogVo;
import com.ruoyi.product.mapper.WoOpLogMapper;
import com.ruoyi.product.service.WoOpLogService;
import com.ruoyi.product.mapper.WoOpLogMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 工单操作日志服务
 */
@Service
public class WoOpLogServiceImpl implements WoOpLogService {

    @Resource
    WoOpLogMapper woOpLogMapper;


    @Override
    public Page<WoOpLogVo> getWoOpLogByPage(WoOpLogDto pageEntity) {
        MPJLambdaWrapper<WoOpLog> wrapper = new MPJLambdaWrapper<WoOpLog>()
                .selectAll(WoOpLog.class)//查询user表全部字段
                //记的测试阶段
                .eq(WoOpLog::getDelFlag, 0)
                .orderByDesc(WoOpLog::getCreateTime);
        wrapper = QueryHelp.getPredicateEx(wrapper, WoOpLog.class, pageEntity);
        Page<WoOpLogVo> userList = woOpLogMapper.selectJoinPage(new Page<>(pageEntity.getCurrentPage(), pageEntity.getPageSize()),   WoOpLogVo.class, wrapper);
        return userList;
    }

    @Override
    public void addWoOpLog(WoOpLogDto testDto) {
        String userName = SecurityUtils.getUsername();
        MPJLambdaWrapper<WoOpLog> wrapper = new MPJLambdaWrapper<WoOpLog>()
                .eq(WoOpLog::getId, testDto.getId())
                .ne(WoOpLog::getDelFlag, 1);
        WoOpLog material = woOpLogMapper.selectOne(wrapper);
        if(null != material) {
            throw new GlobalException("该material已经存在");
        }
        WoOpLog test2 = new WoOpLog();
        BeanUtils.copyProperties(testDto, test2, new String[]{"id"});
        test2.setDelFlag(false);
        test2.setCreateBy(userName);
        test2.setCreateTime(new Date());
        woOpLogMapper.insert(test2);
    }

    @Override
    public void updateWoOpLog(WoOpLogDto testDto) {
        String userName = SecurityUtils.getUsername();
        MPJLambdaWrapper<WoOpLog> wrapper = new MPJLambdaWrapper<WoOpLog>()
                .eq(WoOpLog::getId, testDto.getId());
        WoOpLog woOpLog = woOpLogMapper.selectOne(wrapper);
        if(null == woOpLog) {
            throw new GlobalException("该WoOpLog不存在");
        }
        BeanUtils.copyProperties(testDto, woOpLog);
        woOpLog.setUpdateBy(userName);
        woOpLog.setUpdateTime(new Date());
        woOpLogMapper.updateById(woOpLog);
    }

    @Override
    public void delWoOpLog(WoOpLogDto testDto) {
        WoOpLog material = woOpLogMapper.selectById(testDto.getId());
        if(null == material) {
            throw new GlobalException("该WoOpLog不存在");
        }
        material.setDelFlag(true);
        woOpLogMapper.updateById(material);
    }
}




